package com.longrise.android.byjk.plugins.hra.hratabfirst;

import com.longrise.LEAP.Base.Objects.EntityBean;
import com.longrise.android.byjk.common.UrlConstants;
import com.longrise.android.loaddata.newloaddata.LoadDataManagerFather;
import com.longrise.common.datasource.remote.LoadDataManager;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Administrator on 2018/1/5.
 */

public class HraServiceDetailPresenter extends HraServiceDetailContract.Presenter{

    private List<OrderPlaceParentBean> mParentBeanList = new ArrayList<>();

    @Override
    protected void init() {

    }

    //获取服务点信息
    @Override
    public void getStoreData(String storeId) {
        EntityBean bean = new EntityBean();
        bean.set("id", storeId);
        EntityBean bean1 = new EntityBean();
        bean1.set("bean", bean);
        LoadDataManager.getInstance().callService2(null, UrlConstants.BaseUrl, "hra_getStoreInfo", bean1, new LoadDataManager.OnRequestCallListener2() {

            @Override
            public void onSuccess(String key, String service, Object result) {
                try {
                    EntityBean bean = (EntityBean) result;
                    int resultstate = bean.getInt("resultstate");
                    if(resultstate == 1){
                        mView.refreshStoreData(bean);
                    }else {
                        mView.showToast(bean.getString("resultdesc"));
                    }

                } catch (Exception e) {
                    e.printStackTrace();
                }
            }


            @Override
            public void onFinished(String key, String service) {

            }

            @Override
            public void onError(String key, String service, LoadDataManagerFather.ResultType resultType) {

            }

        });
    }

    //获取服务点机器信息
    @Override
    public void getMachineData(String storeId) {
        EntityBean bean = new EntityBean();
        bean.set("storeid", storeId);
        EntityBean bean1 = new EntityBean();
        bean1.set("bean", bean);
        LoadDataManager.getInstance().callService2(null, UrlConstants.BaseUrl, "hra_getMachineList", bean1, new LoadDataManager.OnRequestCallListener2() {

            @Override
            public void onSuccess(String key, String service, Object result) {
                try {
                    EntityBean bean = (EntityBean) result;
                    int resultstate = bean.getInt("resultstate");
                    if(resultstate == 1){
                        mView.refreshMachineData(bean);
                    }else {
                        mView.showToast(bean.getString("resultdesc"));
                    }

                } catch (Exception e) {
                    e.printStackTrace();
                }
            }


            @Override
            public void onFinished(String key, String service) {

            }

            @Override
            public void onError(String key, String service, LoadDataManagerFather.ResultType resultType) {

            }

        });
    }

    //获取可预约时间
    @Override
    public void getBespeakDate(final String storeId, final String machineid) {

        EntityBean bean = new EntityBean();
        bean.set("storeid", storeId);
        bean.set("machineid", machineid);
        EntityBean bean1 = new EntityBean();


        bean1.set("bean", bean);
        LoadDataManager.getInstance().callService2(null, UrlConstants.BaseUrl, "hra_getBespeakDate", bean1, new LoadDataManager.OnRequestCallListener2() {

            @Override
            public void onSuccess(String key, String service, Object result) {
                try {
                    EntityBean bean = (EntityBean) result;
                    int resultstate = bean.getInt("resultstate");
                    if(resultstate == 1){
                        parseDate(bean,storeId,machineid);
                    }else {
                        mView.showToast(bean.getString("resultdesc"));
                    }

                } catch (Exception e) {
                    e.printStackTrace();
                }
            }


            @Override
            public void onFinished(String key, String service) {

            }

            @Override
            public void onError(String key, String service, LoadDataManagerFather.ResultType resultType) {

            }

        });
    }


    /**
     * 解析可预约日期
     *
     * @param
     */
    public void parseDate(EntityBean bean,String storeid,String machineid) {

        EntityBean[] dataBean = (EntityBean[]) bean.get("result");

        if (dataBean != null) {
            for (EntityBean resultbean : dataBean) {
                String bespeakdate = resultbean.getString("bespeakdate");
                String newdate = resultbean.getString("newdate");
                OrderPlaceParentBean parentBean = new OrderPlaceParentBean();
                parentBean.setBespeakdate(bespeakdate);
                parentBean.setNewDate(newdate);
                parentBean.setStoreid(storeid);
                parentBean.setMachineid(machineid);
                mParentBeanList.add(parentBean);
            }
//            mView.initAdapter(mParentBeanList);
        } else {

        }

    }
}
